/**
 * category
 * tree structrure
 */
'use strict';

var mongodb = require('../mongodb.js');
var Schema = mongodb.Schema;

// create a schema
var ModelSchema = new Schema({
    name: {type: String, unique: true, index: true},
    level: {type: Number, default: 1},
    pName: {type: String, default: null},
    subName: [String],
    createdAt: Date,
    updatedAt: {type: Date, default: Date.now}
});

ModelSchema.pre('save', function (next) {
    if (!this.createdAt) {
        this.createdAt = new Date();
    }
    next();
});

// 指定collection name，默认是复数
var category = mongodb.model('category', ModelSchema, 'category');
exports = module.exports = category;